<?php


namespace app\admin\model;


use think\Db;
use think\Model;

class VpeopleModel extends  Model
{
    protected $name = 'vpeople';

    protected $autoWriteTimestamp = true;   // 开启自动写入时间戳

    /**

     * [getVipsByWhere 根据搜索条件获取vip列表信息]

     * @param $map

     * @param $Nowpage

     * @param $limits

     * @param $od

     * @return bool|\PDOStatement|string|\think\Collection

     * @throws \think\db\exception\DataNotFoundException

     * @throws \think\db\exception\ModelNotFoundException

     * @throws \think\exception\DbException

     */

    public function getVipsByWhere($map, $Nowpage, $limits)

    {

        return $this->where($map)->page($Nowpage, $limits)->select();

    }



    /**

     * [getVipCount 总页面数]

     * @param $map

     * @return int|string

     */

    public function getVipCount($map){

        return $this->where($map)->count();

    }



    /**

     * [getOneNavigation 根据用户id获取一条信息]

     * @param $id

     * @return array|bool|\PDOStatement|string|Model|null

     * @throws \think\db\exception\DataNotFoundException

     * @throws \think\db\exception\ModelNotFoundException

     * @throws \think\exception\DbException

     */

    public function getOneVip($id)

    {

        return $this->where('u_id', $id)->find();

    }



    /**

     * [delVip 删除用户]

     * @param $id

     * @return array

     */

    public function delVip($id)

    {

        Db::startTrans();// 启动事务

        try{

            $this->where('u_id',$id)->update(['is_deldete' => '2']);;

            Db::commit();// 提交事务

            writelog('vip用户【'.$id.'】删除成功',200);

            return ['code' => 200, 'data' => '', 'msg' => 'vpeople用户删除成功'];

        }catch( \Exception $e){

            Db::rollback();// 回滚事务

            writelog('vip用户【'.$id.'】删除失败',100);

            return ['code' => 100, 'data' => $e->getMessage(), 'msg' => 'vpeople用户删除失败'];

        }

    }



    /**

     * [editVip 编辑用户]

     * @param $param

     * @return array

     */

    public function editVip($param)

    {

        Db::startTrans();// 启动事务

        try{

            $this->allowField(true)->save($param, ['u_id' => $param['u_id']]);

            Db::commit();// 提交事务

            writelog('vip用户【'.$param['nick_name'].'】编辑成功',200);

            return ['code' => 200, 'data' => '', 'msg' => 'vip用户编辑成功'];

        }catch( \Exception $e){

            Db::rollback();// 回滚事务

            writelog('vip用户【'.$param['nick_name'].'】编辑失败',100);

            return ['code' => 100, 'data' => $e->getMessage(), 'msg' => 'vip用户编辑失败'];

        }

    }



    /**

     * [checkName 验证用户名称唯一性]

     * @param $username

     * @param $uid

     * @return array

     * @throws \think\db\exception\DataNotFoundException

     * @throws \think\db\exception\ModelNotFoundException

     * @throws \think\exception\DbException

     */

    public function checkName($nick_name,$u_id){

        if($u_id != ''){

            $uname = $this->where('u_id',$u_id)->value('nick_name');

            if($uname == $nick_name){

                return ['code' => 200, 'msg' => 'true'];

            }

        }

        $result = $this->where('nick_name',$nick_name)->find();

        if($result){

            return ['code' => 100, 'msg' => 'false'];

        }else{

            return ['code' => 200, 'msg' => 'true'];

        }

    }



    /**

     * [vipState vip状态]

     * @param $id

     * @return array

     */

    public function vipState($id,$num,$time){

        $nick_name = $this->where('u_id',$id)->value('nick_name');

        if($num == 2){

            $msg = '禁用';

        }else{

            $msg = '启用';

            $time = 0;

        }

        Db::startTrans();// 启动事务

        try{

            $this->where ('u_id' , $id)->where('is_deldete=1')->setField (['is_lock' => $num,'lock_time' => $time]);

            Db::commit();// 提交事务

            writelog('vip【'.$nick_name.'】'.$msg.'成功',200);

            return ['code' => 200, 'data' => '', 'msg' => '已'.$msg];

        }catch( \Exception $e){

            Db::rollback ();//回滚事务

            writelog('vip【'.$nick_name.'】'.$msg.'失败',100);

            return ['code' => 100, 'data' => $e->getMessage(), 'msg' => $msg.'失败'];

        }

    }



    /**

     * [forbiddenVip 批量禁用vip]

     * @param $param

     * @return array

     */

    public function forbiddenVip($param){

        Db::startTrans();// 启动事务

        try{

            if($param){

                $this->saveAll($param);

            }else{

                $this->where('1=1')->where('is_deldete=1')->update(['is_lock'=>2]);

            }

            Db::commit();// 提交事务

            writelog('批量禁用vip成功',200);

            return ['code' => 200, 'data' => '', 'msg' => '批量禁用成功'];

        }catch( \Exception $e){

            Db::rollback ();//回滚事务

            writelog('批量禁用vip失败',100);

            return ['code' => 100, 'data' => $e->getMessage(), 'msg' => '批量禁用失败'];

        }

    }



    /**

     * [usingVip 批量启用vip]

     * @param $param

     * @return array

     */

    public function usingVip($param){

        Db::startTrans();// 启动事务

        try{

            if($param){

                $this->saveAll($param);

            }else{

                $this->where('1=1')->where('is_deldete=1')->update(['is_lock'=>1]);

            }

            Db::commit();// 提交事务

            writelog('批量启用vip成功',200);

            return ['code' => 200, 'data' => '', 'msg' => '批量启用成功'];

        }catch( \Exception $e){

            Db::rollback ();//回滚事务

            writelog('批量启用vip失败',100);

            return ['code' => 100, 'data' => $e->getMessage(), 'msg' => '批量启用失败'];

        }

    }

}